package com.westvalley.syn.service;

import java.util.UUID;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.westvalley.common.kit.SqlsKit;
import com.westvalley.common.kit.StringKit;
import com.westvalley.common.kit.SqlsKit.SqlOperateType;
import com.westvalley.common.result.SqlResult;
import com.westvalley.service.cache.NcbillTypeCache;
import com.westvalley.syn.bean.Result;

/**
 * NC收支项目
 * @author 莫双
 *
 */
public class NcBillTypeService extends NcService{
	private static final String KEY = "nc_billType";
	private NcbillTypeCache ncbillTypeCache = NcbillTypeCache.me();
	
	public NcBillTypeService(){
		refreshCache();
		this.url = prop.getPropValue(KEY);
	}
	
	@Override
	protected Result synMainData(JSONArray jsonArrData) {
		SqlResult sqlResultInsert = SqlResult.me();
		SqlResult sqlResultUpdate = SqlResult.me();
		for(int i=0;i<jsonArrData.size();i++){
			JSONObject ob = jsonArrData.getJSONObject(i);
			if( ob!=null ){
				String billtypecode = ob.getString("billtypecode");	//交易类型主键
				String unitcode = ob.getString("unitcode");			//公司编码
				if( StrKit.notBlank(billtypecode) ){
					Record ncUnitRecord = this.ncbillTypeCache.getRecordByCode(billtypecode+"_"+unitcode);	//缓存中信息
					if(ncUnitRecord==null){
						sqlResultInsert.set(insertData(billtypecode,ob));
					}else{
						sqlResultUpdate.set(updateData(billtypecode,ob));
					}
				}else{
					return Result.me(new RuntimeException("数据主键为空！"));
				}
			}else{
				return Result.me(new RuntimeException("数据解析失败！"));
			}
		}
		int[] insertNums = sqlResultInsert.batch(500);
		int[] updateNums = sqlResultUpdate.batch(500);
		return Result.me(jsonArrData.size(),insertNums,updateNums);
	}

	@Override
	protected Result synpOtherData(Result result, JSONArray jsonArrData) {
		return result;
	}

	@Override
	protected void refreshCache() {
		this.ncbillTypeCache.reload();
	}
	
	/**
	 * 新增数据
	 * @param pk
	 * @param ob
	 * @return
	 */
	private SqlsKit insertData(String pk,JSONObject ob){
		SqlsKit sqlsKit = SqlsKit.me();
		sqlsKit.clear();
		sqlsKit.setTableName("WV_NC_BILLTYPE");
		sqlsKit.addData("id", UUID.randomUUID().toString());
		sqlsKit.addData("PK_BILLTYPE", StringKit.null2Str(ob.getString("billtypecode"))+"_"+StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.addData("billtypecode", StringKit.null2Str(ob.getString("billtypecode")));
		sqlsKit.addData("billtypename", StringKit.null2Str(ob.getString("billtypename")));
		sqlsKit.addData("billstyle", StringKit.null2Str(ob.getString("billstyle")));
		sqlsKit.addData("unitcode", StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.setOperateType(SqlOperateType.INSERT);
		return sqlsKit;
	}
	
	/**
	 * 更新数据
	 * @param pk
	 * @param ob
	 * @return
	 */
	private SqlsKit updateData(String pk,JSONObject ob){
		SqlsKit sqlsKit = SqlsKit.me();
		sqlsKit.clear();
		sqlsKit.setTableName("WV_NC_BILLTYPE");
		sqlsKit.addData("billtypecode", StringKit.null2Str(ob.getString("billtypecode")));
		sqlsKit.addData("billtypename", StringKit.null2Str(ob.getString("billtypename")));
		sqlsKit.addData("billstyle", StringKit.null2Str(ob.getString("billstyle")));
		sqlsKit.addData("unitcode", StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.setWhere("PK_BILLTYPE", StringKit.null2Str(ob.getString("billtypecode"))+"_"+StringKit.null2Str(ob.getString("unitcode")));
		sqlsKit.setOperateType(SqlOperateType.UPDATE);
		return sqlsKit;
	}

}
